const app = getApp();
const root = app.globalData.ROOT_URL;
const utils = require('../../../utils/util.js');
import {searchIcon} from "../../../utils/imgbase64.js";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        listdata:[],
        search:'',
        stype:'',
        url:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            stype:options.stype,
            searchIcon
        })
        if(this.data.stype=="1"){
            this.setData({
                url: root + 'demosts_search'
            })
        } else if (this.data.stype == "2"){
            this.setData({
                url: root + 'groups/' + options.gid +'/replies_search'
            })
            this.detailsId = options.gid;
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    intSearch: function (e) {
        if (e.detail.value !== '') {
            this.setData({
                search: e.detail.value
            })
            this.updateData(this.data.page,this.data.search,false)
        } else {
            this.setData({
                search:'',
                listdata:[]
            })
        }
    },
    back: function () {
        wx.navigateBack()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      let app = getApp();
      app.loginState();
    },
    updateData: function (page,search,state) {
        wx.showLoading({
            title: '',
        })
        if(this.data.stype =="1"){
            this.ajaxData = {
                uid: wx.getStorageSync('user_info').uid,
                page: page,
                search: search
            }
        }else{
            this.ajaxData = {
                page: page,
                search: search
            }
        }
        utils.ajax(this.data.url, {
            header: {
                'Accept': 'application/vnd.dct.v1+json'
            },
            data: this.ajaxData
        }, (res) => {
            wx.hideLoading()
            if (res.state == '1') {
                if (res.data.length < 1) {
                    wx.showToast({
                        title: '没有搜索到更多信息，请稍后重试~',
                        icon: 'none'
                    })
                } else {
                    if (state == true) {
                        this.setData({
                            listdata: this.data.listdata.concat(res.data)
                        })
                    } else {
                        this.setData({
                            listdata: res.data
                        })
                    }
                }
            } else {
                wx.showToast({
                    title: '获取数据失败，网络错误',
                    icon: 'none'
                })
            }
        }, err => {
            wx.hideLoading()
            wx.showToast({
                title: '获取数据失败，网络错误',
                icon: 'none'
            })
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
     
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let p = ++this.data.page;
        this.setData({
            page: p
        })
        this.updateData(this.data.page,this.data.search,true)
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      
    },
     //预览图片
    previewImg: function (e) {
        console.log(e)
        wx.previewImage({
            current: e.currentTarget.dataset.img, // 当前显示图片的http链接
            urls: e.currentTarget.dataset.arr // 需要预览的图片http链接列表
        })
    },
    // 跳转发帖页面(回复)
    reply: function (e) {
        let data = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/views/cardshow/cardshow?posttype=0&gid=' + this.detailsId + '&uid=' + data.uid + '&pid=' + data.pid + '&pcontent=' + data.pcontent + '&pnkname=' + data.pnkname,
        })
    }
})